#include "key.h"
#include "menu_cfg.h"
#include <SPI.h>
#include <TFT_eSPI.h>  
#define TFT_GREY 0xBDF7
#include <menu.h>
#include <menuIO/TFT_eSPIOut.h>
#include <menuIO/serialIO.h>
using namespace Menu;
TFT_eSPI gfx;
int ledCtrl=LOW;
int ledBacklight = 23; 

result myLedOn() {
  ledCtrl= LOW;
  return proceed;
}
result myLedOff() {
  ledCtrl= HIGH;
  return proceed;
}
TOGGLE(ledCtrl,setLed,"Led: ",doNothing,noEvent,noStyle//,doExit,enterEvent,noStyle
  ,VALUE("On",LOW,doNothing,noEvent)
  ,VALUE("Off",HIGH,doNothing,noEvent)
);
int selTest=0;
SELECT(selTest,YearMenu,"Year",doNothing,noEvent,noStyle
  ,VALUE("2023",0,doNothing,noEvent)
  ,VALUE("2022",1,doNothing,noEvent)
  ,VALUE("2021",2,doNothing,noEvent)
  ,VALUE("2020",3,doNothing,noEvent)
  ,VALUE("2019",4,doNothing,noEvent)
  ,VALUE("2018",5,doNothing,noEvent)
);
int chooseTest=1;
CHOOSE(chooseTest,MonthMenu,"Month",doNothing,noEvent,noStyle
  ,VALUE("Jan",1,doNothing,noEvent)
  ,VALUE("Feb",2,doNothing,noEvent)
  ,VALUE("Mar",3,doNothing,noEvent)
  ,VALUE("Apr",4,doNothing,noEvent)
  ,VALUE("May",5,doNothing,noEvent)
  ,VALUE("Jun",6,doNothing,noEvent)
  ,VALUE("Jul",7,doNothing,noEvent)
  ,VALUE("Aug",8,doNothing,noEvent)
  ,VALUE("Sept",9,doNothing,noEvent)
  ,VALUE("Oct",10,doNothing,noEvent)
  ,VALUE("Nov",11,doNothing,noEvent)
  ,VALUE("Dec",12,doNothing,noEvent)
);

//customizing a prompt look!
//by extending the prompt class
class altPrompt:public prompt {
public:
  altPrompt(constMEM promptShadow& p):prompt(p) {}
  Used printTo(navRoot &root,bool sel,menuOut& out, idx_t idx,idx_t len,idx_t) override {
    return out.printRaw(F("special prompt!"),len);;
  }
};

MENU(subMenu,"Winter-break-event",doNothing,noEvent,noStyle
  //,altOP(altPrompt,"",doNothing,noEvent)
  ,OP("ESP32_S2",doNothing,noEvent)
  ,OP("MSP430",doNothing,noEvent)
  ,OP("M1s_Dock",doNothing,noEvent)
  ,OP("STM32_ICE40",doNothing,noEvent)
  ,OP("STEP_Pico",doNothing,noEvent)
  ,EXIT("<Back")
);
const char* constMEM hexDigit MEMMODE="0123456789ABCDEF";
const char* constMEM hexNr[] MEMMODE={"0","x",hexDigit,hexDigit};
char buf1[]="0x11";
MENU(mainMenu,"2023 Happy New Year",doNothing,noEvent,wrapStyle
  //,OP("Op1",doNothing,noEvent)
  //,OP("Op2",doNothing,noEvent)
  ,SUBMENU(subMenu)
  ,SUBMENU(setLed)
  ,OP("LED On",myLedOn,enterEvent)
  ,OP("LED Off",myLedOff,enterEvent)
  ,SUBMENU(YearMenu)
  ,SUBMENU(MonthMenu)
  ,FIELD(ledBacklight,"Day: ","",1,30,10,1,doNothing,noEvent,wrapStyle) // Menu option to set the intensity of the backlight of the screen.
  //,EDIT("Hex",buf1,hexNr,doNothing,noEvent,noStyle)
  ,EXIT("<Back")
);
#define Black RGB565(0,0,0)
#define Red	RGB565(255,0,0)
#define Green RGB565(0,255,0)
#define Blue RGB565(0,0,255)
#define Gray RGB565(128,128,128)
#define LighterRed RGB565(255,150,150)
#define LighterGreen RGB565(150,255,150)
#define LighterBlue RGB565(150,150,255)
#define DarkerRed RGB565(150,0,0)
#define DarkerGreen RGB565(0,150,0)
#define DarkerBlue RGB565(0,0,150)
#define Cyan RGB565(0,255,255)
#define Magenta RGB565(255,0,255)
#define Yellow RGB565(255,255,0)
#define White RGB565(255,255,255)

// TFT color table
const colorDef<uint16_t> display_colors[6] MEMMODE = {
  //{{disabled normal,disabled selected},{enabled normal,enabled selected, enabled editing}}
  {{(uint16_t)Black, (uint16_t)Black}, {(uint16_t)Black, (uint16_t)Red,   (uint16_t)Red}}, //bgColor
  {{(uint16_t)White, (uint16_t)White},  {(uint16_t)White, (uint16_t)White, (uint16_t)White}},//fgColor
  {{(uint16_t)Red, (uint16_t)Red}, {(uint16_t)Yellow, (uint16_t)Yellow, (uint16_t)Yellow}}, //valColor
  {{(uint16_t)White, (uint16_t)White}, {(uint16_t)White, (uint16_t)White, (uint16_t)White}}, //unitColor
  {{(uint16_t)White, (uint16_t)Gray},  {(uint16_t)Black, (uint16_t)Red,  (uint16_t)White}}, //cursorColor
  {{(uint16_t)White, (uint16_t)Yellow}, {(uint16_t)Black,  (uint16_t)Red,   (uint16_t)Red}}, //titleColor
};


#define MAX_DEPTH 13
#define GFX_WIDTH 128
#define GFX_HEIGHT 128
#define fontW 6
#define fontH 9
serialIn serial(Serial0);
idx_t serialTops[MAX_DEPTH]={0};
serialOut outSerial(Serial0,serialTops);

const panel panels[] MEMMODE = {{0, 0, GFX_WIDTH / fontW, GFX_HEIGHT / fontH}};
navNode* nodes[sizeof(panels) / sizeof(panel)]; //navNodes to store navigation status
panelsList pList(panels, nodes, sizeof(panels) / sizeof(panel)); //a list of panels and nodes
idx_t eSpiTops[MAX_DEPTH]={0};
TFT_eSPIOut eSpiOut(gfx,display_colors,eSpiTops,pList,fontW,fontH+1);
menuOut* constMEM outputs[] MEMMODE={&outSerial,&eSpiOut};//list of output devices
outputsList out(outputs,sizeof(outputs)/sizeof(menuOut*));//outputs list controller
//NAVROOT(nav,mainMenu,MAX_DEPTH,serial,out);
NAVROOT(nav,mainMenu,MAX_DEPTH,serial,out);

//when menu is suspended
result idle(menuOut& o,idleEvent e) {
  if (e==idling) {
    o.println(F("suspended..."));
    o.println(F("press [select]"));
    o.println(F("to continue"));
  }
  return proceed;
}

void Btn_loop(void)
{
    if (xSemaphoreTake(timerSemaphore, 0) == pdTRUE){
        portENTER_CRITICAL(&timerMux);
        int my_key =Key_mode;
        portEXIT_CRITICAL(&timerMux);
        switch (my_key)
        {
        case K2_PRESS:
             nav.doNav(escCmd);
             break;
        case EC11_BTN:
             nav.doNav(enterCmd);
             break;
        case EC11_LEFT:
             nav.doNav(upCmd);
             break;
        case EC11_RIGHT:
             nav.doNav(downCmd);
             break;
        default:
            break;
        }

    }

}


void setup() {
 
  Serial0.begin(115200);
  while (!Serial0);
  Serial0.println("MENU CONFIG DEMO");
  Serial0.flush();

  nav.idleTask = idle;//point a function to be used when menu is suspended
  nav.showTitle = true;
  gfx.init();
  gfx.setRotation(0);
  gfx.fillScreen(TFT_GREY);
  gfx.setTextColor(TFT_GREEN, TFT_GREY);  // Adding a black background colour erases previous text automatically
  Serial0.println("gfx init finish");
  pinMode(RED_LED_PIN, OUTPUT);
  digitalWrite(RED_LED_PIN,ledCtrl);
  Serial0.println("led inti finish");
  Adc_Key_init();
}

void loop() {
    Btn_loop();
    nav.poll();
    digitalWrite(RED_LED_PIN,ledCtrl);
}



